package com.meessiom.train.crm;

import com.meessiom.train.crm.collection.model.Collection;
import com.meessiom.train.crm.collection.repository.CollectionRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;

@SpringBootTest
@ActiveProfiles("dev") // 确保只在开发环境运行
public class CollectionDataInitializer {

    @Autowired
    private CollectionRepository collectionRepository;

    /**
     * 测试方法：在数据库中生成100条模拟收款记录
     * 执行后数据将保留在数据库中
     */
    @Test
    public void initializeMockCollections() {
        // 检查是否已有数据
        long existingCount = collectionRepository.count();

        if (existingCount >= 100) {
            System.out.println("数据库中已有 " + existingCount + " 条收款记录，跳过数据生成");
            return;
        }

        // 计算需要生成的数量
        int neededCount = 100 - (int) existingCount;
        System.out.println("开始生成 " + neededCount + " 条模拟收款记录...");

        // 生成并保存模拟数据
        List<Collection> mockCollections = Collection.mock(neededCount);
        collectionRepository.saveAll(mockCollections);

        System.out.println("成功生成 " + neededCount + " 条收款记录");
        System.out.println("数据库现有总数: " + collectionRepository.count());
    }
}